package com.techwells.learningkites.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.animator;
import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseExpandListAdapter;
import com.techwells.learningkites.adapter.DelSlideListAdapter;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.MyFavoriteViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Favorite;
import com.techwells.learningkites.networkservice.model.Page;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.widget.DelSlideListView;
import com.techwells.taco.widget.OnDeleteListioner;

public class MyFavoriteAcitivity extends CommonBaseActivity implements OnDeleteListioner, OnItemClickListener {
	
	private DelSlideListView lvMyFavorite;
	private List<Favorite> list;
	private DelSlideListAdapter adapter;
	
	private MyFavoriteViewModel presentModel;
	private Favorite favorite;
	private int deletePosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myfavorite);
		
//		initTitleBar(R.string.my_favorite, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		lvMyFavorite = (DelSlideListView) findViewById(R.id.lv_my_favorite);
//		list =  new ArrayList<Favorite>();
//		for (int i = 0; i < 6; i++) {
//			Favorite f = new Favorite();
//			list.add(f);
//		}
//		adapter =  new DelSlideListAdapter(this, list);
//		lvMyFavorite.setAdapter(adapter);
//		lvMyFavorite.setDeleteListioner(this);
//		lvMyFavorite.setOnItemClickListener(this);
		
//		adapter.setOnDeleteListioner(this);
	}

	@Override
	public boolean isCandelete(int position) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onDelete(int position) {
		deletePosition = position;
		doRequestDeleteFavorite(favorite.data.get(position).id);
	}

	@Override
	public void onBack() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//		Route.route().nextController(this, VideoDetailActivity.class.getName(), Route.WITHOUT_RESULTCODE);
		Intent intent = new Intent(this, VideoDetailActivity.class);
		intent.putExtra("videoId", favorite.data.get(arg2).id);
		Route.route().nextControllerWithIntent(this, VideoDetailActivity.class.getName(), Route.WITHOUT_RESULTCODE,intent);
	}
	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		
		presentModel = (MyFavoriteViewModel) baseViewModel;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		doRequestGetFavorite();
	}

	private void doRequestGetFavorite() {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("l", 15);
		argMap.put("n", 1);
		argMap.put("o", "id");
		argMap.put("s", "desc");
		doTask(KitesServiceMediator.SERVICE_GET_FAVORITE_VIDEOS,argMap);
		
	}
	
	private void doRequestDeleteFavorite(int videoId) {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("videoId", videoId);
		doTask(KitesServiceMediator.SERVICE_DELETE_FAVORITE_VIDEO,argMap);
		
	}
	
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_FAVORITE_VIDEOS)) {
			favorite = ((MyFavoriteViewModel)baseViewModel).favorite;
			adapter =  new DelSlideListAdapter(this, favorite.data);
			lvMyFavorite.setAdapter(adapter);
			lvMyFavorite.setDeleteListioner(this);
			lvMyFavorite.setOnItemClickListener(this);
			adapter.setOnDeleteListioner(this);
			
			initTitleBar(getResources().getString(R.string.my_favorite) + "( "+favorite.data.size() + " )", defaultLeftClickListener);
			
		}else if (method.equals(KitesServiceMediator.SERVICE_DELETE_FAVORITE_VIDEO)) {
			int returnCode = ((MyFavoriteViewModel)baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				list.remove(deletePosition);
				lvMyFavorite.deleteItem();
				adapter.notifyDataSetChanged();
			}
			
		}
	}

}


